import Vue from 'vue'
import Router from 'vue-router'
import Login from './views/Login.vue'
import Register from './views/Register.vue'


import Layout from './views/Layout.vue'

Vue.use(Router)

let router = new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/register',
      name: 'register',
      component: Register
    },
    {
      path: '/',
      name: 'home',
      component: Layout,
      redirect:'/home',
      children: [
        // 展示在右侧的路由
        {path:'home', component:()=> import('./views/Home.vue')   },
        {path:'markdown', component:()=> import('./views/Markdown.vue') },
      ]
    },
    {
      path: '/about',
      name: 'about',
      component: Layout
    }
  ]
})
// 导航守卫
router.beforeEach((to, from, next) => {
  //to and from 都是路由对象,next() 必须被调用才能触发(reslove)钩子函数

  // 判断用户是否登陆（cookie/vuex） 
  // let token = getCookie('token')

  let token = null; // 无登陆
  if( !token && !( to.path == '/login' || to.path == '/register' ) ){
    console.log('not login or register');
    // 如无登陆，强制到登陆页
    next('/login');
  }else{
    next();
  }
})


export default router;